package org.vectrics.skill.struts;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.skill.Skill;
import org.vectrics.skill.SkillRating;
import org.vectrics.skill.SkillRatingSet;
import org.vectrics.skill.SkillSetUtil;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;


abstract public class PrepareWizardActionAbstract extends VectricsStrutsAction {
    private Logger log = Logger.getLogger(this.getClass());
    abstract protected boolean getNeedToAddImpliedSkills();
    abstract protected SkillRatingSet getSkillRatingSet(Request request, Person authPerson);

    
    /**
     * This action prepares the user profile for entering the skill wizard.
     */
    public String doExecute(VectricsActionData actionData) {
        log.debug("doExecute() called.");
    	SkillWebUtils.removeSessionSkillRatingSet(actionData.getRequest());
        
        String skillSection = actionData.getRequest().getParameter("skillSection");
        SkillWebUtils.setSkillSection(actionData.getRequest(), skillSection);
        SkillWebUtils.setLevel(actionData.getRequest(), 0);
        if (actionData.getForm() == null) {
        	SkillRatingForm ratingForm = new SkillRatingForm();
        	actionData.setForm(ratingForm);
        }
        SkillRatingSet skillRatingSet = null;
        
    	skillRatingSet = getSkillRatingSet(actionData.getRequest(), actionData.getAuthPerson());
    	SkillWebUtils.setSkillRatingSet(actionData.getRequest(), skillRatingSet);
        
        Skill rootSkill = null;
       	rootSkill = RecruitServiceLocator.getSkillProvider().getRootSkill();
       	if (rootSkill == null)
       		throw new SystemException("Root skill is null");
       	if (skillRatingSet == null)
       		throw new SystemException("skill rating set is null");
        	
        if (getNeedToAddImpliedSkills()) {
            try {
                SkillSetUtil util = new SkillSetUtil(skillRatingSet, rootSkill);
                util.addImpliedParentSkills();
            } catch (Exception ex) {
                log.error("Exception getting user skills.", ex);
                throw new SystemException("Exception getting user skills.", ex);
            }
        } else {
        	if (log.isDebugEnabled())
        		log.debug("doPerform() - adding implied skills to job. Before adding implied, count = " + skillRatingSet.findAllSkillRatings().size());
            	SkillSetUtil util = new SkillSetUtil(skillRatingSet, rootSkill);
            	util.addImpliedParentSkills();
        }
        
        if (log.isDebugEnabled()) {
        	Iterator iter = skillRatingSet.getSkillRatings().iterator();
        	while (iter.hasNext()) {
        		SkillRating rating = (SkillRating)iter.next();
        		log.debug(rating.getSkill().getCode() + "  -  " + rating.getDescription());
        	}
        }
        return ("success");
    }


}